// 封装一个整套的数据库增删改查操作
var mongodb = require("mongodb");
var MongoClient = mongodb.MongoClient //mongodb 客户端
var url = "mongodb://127.0.0.1:27017" //数据库服务器地址
var dbName = "vuetest" //连接的库名
    // 查找方法

var find = function(client, collection, selector, callback) {
        collection.find(selector).toArray(function(err, result) {
            if (err) throw err
            console.log("查找成功")
            console.log(result)
            callback(result)
            client.close() //释放与数据库服务器连接
        })
    }
    // 添加一个
var add = function(client, collection, selector, callback) {
        collection.insertOne(selector, function(err, result) {
            if (err) throw err;
            console.log("添加成功")
            callback(result.result) //{n:1,ok:1}
            client.close()
        })
    }
    // 删除一个
var del = function(client, collection, selector, callback) {
        collection.deleteOne(selector, function(err, result) {
            if (err) throw err;
            console.log("删除成功")
            callback(result.result) //{n:1,ok:1}
            client.close()
        })
    }
    // 修改一个
var update = function(client, collection, selector, callback) {
        collection.updateOne(selector[0], { $set: selector[1] }, function(err, result) {
            if (err) throw err;
            console.log("修改成功")
            callback(result.result) //{n:1,ok:1}
            client.close()
        })
    }
    // 添加多个
var insertMany = function(client, collection, selector, callback) {
        collection.insertMany(selector, function(err, result) {
            if (err) throw err;
            console.log("添加多个成功")
            callback(result.result) //{n:1,ok:1}
            client.close()
        })
    }
    // 删除多个
var delMany = function(client, collection, selector, callback) {
        collection.deleteMany(selector, function(err, result) {
            if (err) throw err;
            console.log("删除多个成功")
            callback(result.result) //{n:1,ok:1}
            client.close()
        })
    }
    // 修改多个
var updateMany = function(client, collection, selector, callback) {
        collection.updateMany(selector[0], { $set: selector[1] }, function(err, result) {
            if (err) throw err;
            console.log("修改多个成功")
            callback(result.result) //{n:1,ok:1}
            client.close()
        })
    }
    // 排序方法
var sorts = function(client, collection, selector, callback, sortSel) {
    collection.find(selector).sort(sortSel).toArray(function(err, result) {
        if (err) throw err;
        console.log("排序查找成功")
        callback(result) //{n:1,ok:1}
        client.close()
    })
}



var methodType = {
    find: find,
    add: add,
    del: del,
    update: update,
    insertMany: insertMany,
    delMany: delMany,
    updateMany: updateMany,
    sorts: sorts
}


// type 操作方法
// collection 表名
// selector 操作条件
// callback 回调函数
module.exports = function(type, collection, selector, callback, sortSel) {
    MongoClient.connect(url, function(err, client) {
        var db = client.db(dbName) //连接数据库
        var collections = db.collection(collection) //链接到表
        methodType[type](client, collections, selector, callback, sortSel)
    })
}